package com.tutu.mybatis.dynamic.datasource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutu.mybatis.dynamic.datasource.annotation.DynamicDatasource;
import com.tutu.mybatis.dynamic.datasource.bean.Balance;
import com.tutu.mybatis.dynamic.datasource.mapper.BalanceMapper;
import com.tutu.mybatis.dynamic.datasource.service.BalanceService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author qyy
 * @Date 2020/9/25 10:42
 */
@Service
@Slf4j
@DynamicDatasource
@CacheConfig(cacheNames = "balance")
public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> implements BalanceService {

    private static final Logger logger = LoggerFactory.getLogger(BalanceServiceImpl.class);

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict()
    public boolean save(Balance balance) {
        logger.info("balance:{}", balance);
        boolean flag = super.save(balance);
        if (flag) {
            throw new RuntimeException("手动异常");
        }
        return false;
    }

    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public List<Balance> list() {
        return super.list();
    }
}
